<?php

namespace app\controller\uc;

use app\bll\ComponentWidget as BllComponentWidget;
use app\bll\UcenterIndexLayout as BllUcenterIndexLayout;
use core\controller\UcBase;
use Exception;
use support\View;

class Index extends UcBase
{
    public function index()
    {
        if (is_post()) {
            $optype = request()->get('optype', '');
            switch ($optype) {
                case 'selWidget': //获取挂件
                    $bllComponentWidget = new BllComponentWidget();
                    $widgets = $bllComponentWidget->paginator(type: 2);
                    return $this->success(['widgets' => $widgets]);
                    break;
                case 'chooseWidget': //选择挂件
                    return $this->success(['widget' => widget('uc/' . request()->input('widgetident'))]);
                    break;
                case 'saveLayout': //保存布局

                    try {
                        $layouts               = htmlspecialchars_decode(request()->post('layouts'));
                        $bllUcenterIndexLayout = new  BllUcenterIndexLayout();
                        $bllUcenterIndexLayout->setLayout(json_decode($layouts, true));
                    } catch (Exception $ex) {
                        return $this->error($ex->getMessage());
                    }
                    return $this->success();
                    break;
            }
        } else {

            return $this->view();
        }
    }
}
